package com.yeyks.controller.room;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.LoginUser;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.param.UpdateVipRoomParam;
import com.yeyks.commonReference.service.hotel.RoomInfoService;
import com.yeyks.hotel.dal.domain.RoomCalendarVipInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.param.UpdateRoomCalendarBatchParam;
import com.yeyks.hotel.service.RoomCalendarVipInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "VIP甄选酒店房间模块")
@RestController
@Slf4j
@RequestMapping("/admin/room/vip")
public class VIPRoomController {

    @Autowired
    private RoomCalendarVipInfoService roomCalendarVipInfoService;
    @Autowired
    private RoomInfoService roomInfoService;

    @ApiOperation(value = "查询VIP房态信息", notes = "查询VIP房态信息")
    @RequestMapping(value = "/selectRoomVIPCalendar", method = RequestMethod.POST)
    public AjaxResponseBody selectRoomVIPCalendar(@RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId) {

        List<RoomCalendarVipInfo> list = roomCalendarVipInfoService.selectVIPRoomCalendar(roomId);

        return AjaxResponseBody.success(list);
    }

    @ApiOperation(value = "修改甄选房间配置信息", notes = "修改甄选房间配置信息")
    @RequestMapping(value = "/updateVIPRoomConfig", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateVIPRoomConfig(
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "salePrice", defaultValue = "", required = true) Integer salePrice,
            @RequestParam(name = "purcPrice", defaultValue = "", required = true) Integer purcPrice,
            @RequestParam(name = "count", defaultValue = "", required = true) Integer count) {

        RoomInfo roomInfo = roomInfoService.getById(roomId);

        if (null == roomInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomInfo.setVipDiscountPrice(salePrice);
        roomInfo.setVipPrice(purcPrice);
        roomInfo.setVipCount(count);
        boolean result = roomInfoService.updateById(roomInfo);
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "修改单日VIP房态信息", notes = "修改单日VIP房态信息")
    @RequestMapping(value = "/updateRoomVIPCalendarSingle", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomVIPCalendarSingle(
            @RequestParam(name = "id", defaultValue = "", required = true) Integer id,
            @RequestParam(name = "salePrice", defaultValue = "", required = true) Integer salePrice,
            @RequestParam(name = "purcPrice", defaultValue = "", required = true) Integer purcPrice,
            @RequestParam(name = "count", defaultValue = "", required = true) Integer count,
            @RequestParam(name = "status", defaultValue = "", required = true) Integer status) {

        RoomCalendarVipInfo roomCalendarVipInfo = roomCalendarVipInfoService.getById(id);

        if (null == roomCalendarVipInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomCalendarVipInfo.setSalePrice(salePrice);
        roomCalendarVipInfo.setPurcPrice(purcPrice);
        roomCalendarVipInfo.setCount(count);
        roomCalendarVipInfo.setStatus(status);
        boolean result = roomCalendarVipInfoService.updateById(roomCalendarVipInfo);
        //上架操作则上架房间
        if (result && status == 1) {
            RoomInfo roomInfo = roomInfoService.selectByPrimaryKey(roomCalendarVipInfo.getRoomId());
            roomInfo.setVipStatus(1);
            roomInfoService.updateById(roomInfo);
        }
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "批量更新VIP房态信息", notes = "批量更新VIP房态信息")
    @RequestMapping(value = "/updateRoomVIPCalendarBatch", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomVIPCalendarBatch(UpdateRoomCalendarBatchParam updateRoomCalendarBatchParam, @ApiIgnore @LoginUser AccountInfo accountInfo) {

        if (null == accountInfo || !accountInfo.getType().equals(AccountType.Administrator)) {
            return AjaxResponseBody.failed("非公司账号,不允许操作");
        }
        updateRoomCalendarBatchParam.setHotelId(updateRoomCalendarBatchParam.getHotelId());
        return roomCalendarVipInfoService.updateRoomCalendarBatch(updateRoomCalendarBatchParam);
    }

    @ApiOperation(value = "初始化房态信息", notes = "初始化房态信息")
    @RequestMapping(value = "/initVIPRoomCalendar", method = RequestMethod.POST)
    public AjaxResponseBody initVIPRoomCalendar(
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "days", defaultValue = "60", required = true) Integer days) {

        RoomInfo roomInfo = roomInfoService.selectByPrimaryKey(roomId);
        if (null != roomInfo) {
            roomCalendarVipInfoService.initVIPRoomCalendar(roomInfo);
            return AjaxResponseBody.success(roomInfo);
        }
        return AjaxResponseBody.failed("失败");
    }

    @ApiOperation(value = "设置成甄选/精选房间", notes = "设置成甄选/精选房间")
    @RequestMapping(value = "/updateVipRoom", method = RequestMethod.POST)
    @SystemLog(SysLogMsg.VIP_ROOM_SET)
    public AjaxResponseBody toBeVipRoom(UpdateVipRoomParam updateVipRoomParam) {
        return roomInfoService.toBeVipRoom(updateVipRoomParam);
    }

    @ApiOperation(value = "取消甄选房间", notes = "取消甄选房间")
    @RequestMapping(value = "/cancelVipRoom", method = RequestMethod.POST)
    @SystemLog(SysLogMsg.VIP_ROOM_CANCLE)
    public AjaxResponseBody cancelVipRoom(Integer roomId) {
        return roomInfoService.cancelVipRoom(roomId);
    }

    @ApiOperation(value = "甄选房间上下架", notes = "甄选房间上下架")
    @RequestMapping(value = "/updateVipRoomStatus", method = RequestMethod.POST)
    @SystemLog(SysLogMsg.VIP_ROOM_VALID)
    public AjaxResponseBody updateVipRoomStatus(Integer roomId, Integer status) {
        return roomInfoService.updateVipRoomStatus(roomId, status);
    }

    @ApiOperation(value = "甄选用房间信息", notes = "甄选用房间信息")
    @RequestMapping(value = "/selectAllRoom", method = RequestMethod.POST)
    public AjaxResponseBody selectAllRoom(Integer hotelId) {
        List<RoomInfo> vipList = roomInfoService.queryRoomListByHotelId(hotelId, 1);
        List<RoomInfo> listC2B = roomInfoService.queryRoomListByHotelId(hotelId, 0);
        vipList.addAll(listC2B);
        return AjaxResponseBody.success(vipList);
    }
}
